package com.miluGame.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.RespectBinding;

import org.apache.commons.beanutils.converters.CalendarConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import sun.util.resources.CalendarData;

import com.miluGame.entity.User;
import com.miluGame.service.UserService;
import com.miluGame.util.Util;
import com.sun.istack.internal.NotNull;

@Controller
public class UserController {
	
	@Autowired
	private UserService userService;
	
	
	@RequestMapping("login.go")
	public String login(String name,String password,HttpSession session,HttpServletRequest rsq,HttpServletResponse rsp) {
		try {
			User user = userService.login(name, password, session, rsq, rsp);
			if(user!=null){
				session.setAttribute("loginUser", user);
				rsq.setAttribute("user", user);
				return "redirect:jsp/main.jsp";
			}else{
				Util.alert(rsp, "用户名或者密码错误,请重新输");
				return "redirect:login.jsp";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "redirect:login.jsp";
		}
		
	}
	
	
	@RequestMapping("logout.go")
	public String logout(HttpSession session){
		session.removeAttribute("user");
		return "login";
	}
	
	//用户管理界面
	@RequestMapping("user/userManager.go")
	public String userManager(HttpSession session,HttpServletRequest req,HttpServletResponse rsp){
		userService.queryUserPage(session, req, rsp);
		return "WEB-INF/user/userManager";
	}
	
	
	//添加用户
	@RequestMapping("user/addUser.go")
	public String addUser(String name,String password,HttpServletResponse rsp){
			try {
				if(name!=null&&password!=null){
					User user = new User();
					user.setUserName(name);
					user.setUserPwd(password);
					user.setDisabled(0);
					userService.add(user);
				}else{
					Util.alert(rsp, "用户名或密码不能为空");
				}
			} catch (Exception e) {
				Util.alert(rsp, "添加失败");
				e.printStackTrace();
			}
		return "../user/userManager.go";
	}

	
	@RequestMapping("user/delUser.go")
	@ResponseBody
	public String delUser(HttpServletRequest rsq){
		String userId = rsq.getParameter("userId");
		String status = "success";
		if(userId!=null){
			try {
				userService.delete(userId);
				status= "success";
			} catch (Exception e) {
				status = "fail";
				e.printStackTrace();
			}
		}
		return status;
	}
	
	
	@RequestMapping("user/editUser.go")
	public String updateUserInfo(HttpServletRequest req,HttpServletResponse rsp){
		String userName = req.getParameter("userName");
		Integer able = Integer.parseInt(req.getParameter("disabled").toString());
		Integer userId = Integer.parseInt(req.getParameter("userId").toString());
		try {
			userService.updateUser(userName,able,userId);
		} catch (Exception e) {
			//Util.out(rsp, "修改失败");
			e.printStackTrace();
		}
		return "redirect:../user/userManager.go";
	} 
}
